package com.deluxelx.rmo.messageboard.controller;

import cn.hutool.core.util.ObjectUtil;
import com.deluxelx.rmo.common.enums.ResponseCode;
import com.deluxelx.rmo.messageboard.api.PostService;
import com.deluxelx.rmo.messageboard.api.dto.DiscussPostDto;
import com.deluxelx.rmo.messageboard.api.req.DiscussPostReq;
import com.deluxelx.rmo.messageboard.api.req.PraiseNumReq;
import com.deluxelx.rmo.messageboard.api.res.MessageBoardRes;
import com.deluxelx.rmo.user.api.dto.UserPostDto;
import com.deluxelx.rmo.user.api.rpc.UserPostService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/discuss_post")
public class DiscussPostController {

    @DubboReference
    private UserPostService userPostService;

    @Resource
    private PostService postService;

    @PostMapping("/send")
    public MessageBoardRes<String> sendPost(@RequestBody DiscussPostReq discussPostReq) {
        if (ObjectUtil.isNull(discussPostReq.getUserId()))
            return MessageBoardRes.buildErrorResult(ResponseCode.UN_LOGIN);

        UserPostDto userPostInfo = userPostService.getUserPostInfo(discussPostReq.getUserId());
        return (ObjectUtil.isNull(userPostInfo))
                ? MessageBoardRes.buildErrorResult(ResponseCode.USER_STATUS_EXCEPTION, "用户账户异常！请待会再重试！")
                : postService.sendPost(discussPostReq, userPostInfo);
    }

    @GetMapping("/getRecentPostList")
    public MessageBoardRes<List<DiscussPostDto>> getRecentPostList() {
        return MessageBoardRes.buildSuccessResult(postService.getRecentPostList());
    }

    @PutMapping("/addPraiseNum")
    public MessageBoardRes<String> addPraiseNum(@RequestBody PraiseNumReq praiseNumReq) {
        return null;
    }

}
